package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.GroupMainModel;
import com.hangzhou.xyj.model.GroupModel;
import com.hangzhou.xyj.ui.widget.CustomGridView;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo.duan on 2016/1/6.
 * 圈子头部
 */
public class GroupHeadersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<GroupModel> mNewDatas = new ArrayList<GroupModel>();
    private List<GroupModel> mExpertDatas = new ArrayList<GroupModel>();
    private List<GroupModel> mRecommendDatas = new ArrayList<GroupModel>();
    private List<GroupModel> mNearbyDatas = new ArrayList<GroupModel>();
    private GroupMainModel mGroupMainData;
    private Intent mIntent;


    public GroupHeadersAdapter(Context context, GroupMainModel groupMain) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGroupMainData = groupMain;
    }

    /**
     * @param
     * @return
     * @Description 更新数据
     * @author yaohuix
     * @date 2016/3/3 14:14
     */
    public void update(GroupMainModel groupMainModel) {
        if (groupMainModel == null) {
            groupMainModel = new GroupMainModel();
        }
        this.mGroupMainData = groupMainModel;
        mNewDatas = mGroupMainData.getNewGroup();
        mExpertDatas = mGroupMainData.getPopularGroup();
        mRecommendDatas = mGroupMainData.getRecommendGroup();
        mNearbyDatas = mGroupMainData.getNearGroup();
        notifyDataSetChanged();
    }

    /**
     * 划分不同的区域：新兴圈子，名家部落，推荐圈子，附近圈子
     *
     * @param position the position of the view to get the header ID of
     * @return
     */
    @Override
    public long getHeaderId(int position) {
        return position;
    }

    /**
     * item headerView的创建
     *
     * @param viewGroup the view to create a header view holder for
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        HeaderViewHolder viewHolder = new HeaderViewHolder(mLayoutInflater.inflate(R.layout.layout_group_item_header, viewGroup, false));
        return viewHolder;
    }

    /**
     * 头部数据绑定
     *
     * @param holder   the view holder
     * @param position the adapter position
     */
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeaderViewHolder viewHolder = (HeaderViewHolder) holder;
        switch (position) {
            case 0:
                viewHolder.mTvHeader.setText("新兴圈子");
                break;
            case 1:
                viewHolder.mTvHeader.setText("演员部落");
                break;
            case 2:
                viewHolder.mTvHeader.setText("推荐圈子");
                break;
            case 3:
                viewHolder.mTvHeader.setText("附近圈子");
                break;
        }

    }

    /**
     * 正常条目item个数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return 4;
    }

    /**
     * View类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /**
     * 正常条目item创建
     *
     * @param viewGroup
     * @param viewType  view的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0:
            case 1:
                viewHolder = new LinearViewHolder(mLayoutInflater.inflate(R.layout.layout_linear_group, viewGroup, false));
                break;
            case 2:
            case 3:
                viewHolder = new GridViewHolder(mLayoutInflater.inflate(R.layout.layout_grid_group, viewGroup, false));
                break;
            default:
                viewHolder = new LinearViewHolder(mLayoutInflater.inflate(R.layout.layout_linear_group, viewGroup, false));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (position) {
            case 0:
                ((LinearViewHolder) viewHolder).listView.setAdapter(new NewGroupAdapter(mContext, mNewDatas));//设置新兴圈子的数据
                ((LinearViewHolder) viewHolder).listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        IntentManager.getInstance().goGroupDetailActivity(mContext, mNewDatas.get(position).getGroupCode());
                    }
                });
                break;
            case 1:
                ((LinearViewHolder) viewHolder).listView.setAdapter(new NewGroupAdapter(mContext, mExpertDatas));//设置名家部落的数据
                ((LinearViewHolder) viewHolder).listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        IntentManager.getInstance().goGroupDetailActivity(mContext, mExpertDatas.get(position).getGroupCode());
                    }
                });
                break;
            case 2:
                ((GridViewHolder) viewHolder).gridView.setAdapter(new RecommendGroupAdapter(mContext, mRecommendDatas));//需要传入推荐圈子的数据
                ((GridViewHolder) viewHolder).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        IntentManager.getInstance().goGroupDetailActivity(mContext, mRecommendDatas.get(position).getGroupCode());
                    }
                });
                break;
            case 3:
                ((GridViewHolder) viewHolder).gridView.setAdapter(new NearbyGroupAdapter(mContext, mNearbyDatas));//需要传入附近圈子的数据
                ((GridViewHolder) viewHolder).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        IntentManager.getInstance().goGroupDetailActivity(mContext, mNearbyDatas.get(position).getGroupCode());
                    }
                });
                break;
        }

    }


    /**
     * 线性布局的ViewHolder
     */
    class LinearViewHolder extends RecyclerView.ViewHolder {
        ListView listView;

        public LinearViewHolder(View view) {
            super(view);
            listView = (ListView) itemView.findViewById(R.id.rv_group);
        }
    }

    /**
     * Grid布局的ViewHolder
     */
    class GridViewHolder extends RecyclerView.ViewHolder {
        CustomGridView gridView;

        public GridViewHolder(View view) {
            super(view);
            gridView = (CustomGridView) itemView.findViewById(R.id.rv_group);
        }
    }

    /**
     * Headr的ViewHolder
     */
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        public MyTextView mTvHeader;
//        public MyTextView mTvMore;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            mTvHeader = (MyTextView) itemView.findViewById(R.id.tv_header_group);
//            mTvMore = (MyTextView) itemView.findViewById(R.id.tv_more);
        }
    }
}
